//
// Created by ASUS on 2023/12/29.
//

#include <bits/stdc++.h>
using namespace std;

int longestCycle(vector<int>& edges) {
	int n=edges.size(),time[n],ans=-1;
	memset(time,0,sizeof(time));
	for(int i=0,colok=1;i<n;++i){
		if(time[i]) continue;
		for(int x=i,start_time=colok;x>=0;x=edges[x]){
			if(time[x]){
				if(time[x]>=start_time){
					ans=max(ans,colok-time[x]);
				}
				break;
			}
			time[x]=colok++;
		}
	}
	return ans;
}